﻿#include "jiuxing_steel_rope_broken.h"

// 使用 utf8 编码
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif

// 玖行 钢丝断股

/// <summary>
/// 
/// </summary>
jiuxing_steel_rope_broken::jiuxing_steel_rope_broken()
{
	m_pSteelDet = nullptr;
	m_pSteeRectDefect = nullptr;
}


/// <summary>
/// 
/// </summary>
jiuxing_steel_rope_broken::~jiuxing_steel_rope_broken()
{

	if (m_pSteelDet != nullptr)
	{
		m_pSteelDet->Release();
		m_pSteelDet = nullptr;
	}

	if (m_pSteeRectDefect != nullptr)
	{
		m_pSteeRectDefect->Release();
		m_pSteeRectDefect = nullptr;
	}

}


/// <summary>
/// 
/// </summary>
/// <returns></returns>
void jiuxing_steel_rope_broken::init()
{
	m_pSteelDet = CreateDetObj(m_strJX_SteelDetModelPath);
	m_pSteeRectDefect = CreateDetObj(m_strJX_SteelRectDefectModelPath);
}


/// <summary>
/// 
/// </summary>
/// <param name="pData"></param>
void jiuxing_steel_rope_broken::execute(QSharedPointer<IMG_DATA> pData, int nBuffersize)
{
	IMG_DATA data = *pData;
	data.image = urlToPixmap(data.strUrl);
	saveImage(data);

	cv::Mat image = QImage2cvMat(data.image);
	int nDispId = data.nDispId;

	if (!image.data || m_pSteelDet == nullptr)
	{
		return;
	}


	QString strDate = QDateTime::currentDateTime().toString("yyyy-MM-dd");
	QString strTime = QDateTime::currentDateTime().toString("hh-mm-ss");



	std::vector<DetectResult> results;
	m_pSteelDet->Evaluate(image, results);

	// 保存图片
	saveImage(image, "玖行钢索(定位)", data, results.size());
	

	for (int i = 0; i < results.size(); i++)
	{
		DetectResult res = results[i];
		cv::Rect rect = res.box;
		rect &= cv::Rect(0, 0, image.cols, image.rows);//求交集'

		int x1 = rect.tl().x;
		int y1 = rect.tl().y;
		int x2 = rect.br().x;
		int y2 = rect.br().y;
		int x = (x2 - x1) * 0.5 + x1;
		int w = 900;
		cv::Rect rect1(x - w/2, 0, w, y2);
		results[i].box = rect1;

		cv::Mat image_roi = image(rect1);

		

		std::vector<DetectResult> def_results;
		if (m_pSteeRectDefect != nullptr)
		{
			//m_pSteeRectDefect->Evaluate(image_roi, def_results);
		}

		// 保存图片
		saveImage(image_roi, "玖行钢索断股", data, def_results.size());

		// 如果没检测到破损，则不发布破损相关的数据
		if (def_results.size() != 0)
		{
			// 返回结果到 kafka 
			_tagResults tagResults;
			tagResults.setImageData(data);
			tagResults.setResults("钢索断股", def_results, res.box.x, res.box.y);
			tagResults.setVersion("");

			QString strResultTopc = data.strTopicName + suffix_2;
			QString strResultData = tagResults.toJSon();

			pushKafka(strResultTopc, strResultData, data.strStationId);
		}


		QPainter painter(&data.image);
		
		if (def_results.size() == 0)
		{
			painter.setBrush(QBrush(QColor(0, 255, 0, 50)));
			QPen pen(QColor(0, 255, 0));
			pen.setWidth(5);
			painter.setPen(pen);
		}
		else
		{
			painter.setBrush(QBrush(QColor(255, 0, 0, 50)));
			QPen pen(Qt::red);
			pen.setWidth(5);
			painter.setPen(pen);
		}

		cv::Point point = getCenterPoint(rect1);
		painter.drawRect(rect1.tl().x, rect1.tl().y, rect1.width, rect1.height);

		emit updateShow(data.nDispId, data.image, nBuffersize);
	}


	// 返回结果到 kafka 
	_tagResults tagResults;
	tagResults.setImageData(data);

	tagResults.setResults("钢索", results);
	tagResults.setVersion(m_strJX_SteelDetModelPath);


	QString strResultTopc = data.strTopicName + suffix_1;
	QString strResultData = tagResults.toJSon();

	pushKafka(strResultTopc, strResultData, data.strStationId);
}
